package com.atguigu.controller;

import com.atguigu.constant.MessageConstant;
import com.atguigu.constant.RedisConstant;
import com.atguigu.constant.RedisMessageConstant;
import com.atguigu.entity.Result;
import com.atguigu.util.SMSUtils;
import com.atguigu.util.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {

    @Autowired
    JedisPool jedisPool;

    @RequestMapping("/send4Order.do")
    public Result send4Order(String tel) throws Exception {
        Integer code = ValidateCodeUtils.generateValidateCode(4);
        SMSUtils.sendShortMessage(tel,code.toString());
        jedisPool.getResource().setex(tel+ RedisConstant.SETMEAL_PIC_RESOURCES,60*60*24,code.toString());
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }

    @RequestMapping("/send4Login.do")
    public Result send4Login(String telephone){
        //1.生成验证码
        try {
            Integer code = ValidateCodeUtils.generateValidateCode(4);
            //2.发送
            SMSUtils.sendShortMessage(telephone,code.toString());
            //3.redis保存
            jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_LOGIN,60,code.toString());
            return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }
}
